package com.qf.controller;

import com.qf.dto.DeveloperDto;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.page.Result;
import com.qf.service.DeveloperService;
import com.qf.vo.DeveloperVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/developer")
@RestController
public class DeveloperController {

    @Autowired
    private DeveloperService developerService;



    /**
     * project工程列表分页查询+模糊查询
     * @param queryPageBean
     * @return PageResult
     */
    @RequestMapping("/all")
    public PageResult all(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = developerService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 新建开发商并绑定工程
     * @param developerVO
     * @return Result
     */
    //前端传json
    @RequestMapping("/add")
    public Result add(@RequestBody DeveloperVo developerVO) {
        Result result = new Result();
        boolean b = developerService.add(developerVO);
        if (b){
            result.setFlag(true);
            result.setMessage("添加成功");
            return result;
        }
        result.setFlag(false);
        result.setMessage("添加失败");
        return result;
    }


    /**
     * 编辑project回显数据并显示绑定的cids
     * @param did
     * @return Result
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam("did") String did ){
        Result result = new Result();
        DeveloperDto developerDto = developerService.findById(did);
        boolean b = ObjectUtils.isEmpty(developerDto);
        if (!b){
            result.setData(developerDto);
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }else {
            result.setFlag(false);
            result.setMessage("fail");
            return result;
        }
    }


    /**
     * 修改developer
     * @param developerVo
     * @return result
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody DeveloperVo developerVo){
        Result result = new Result();
        int i = developerService.edit(developerVo);
        if (i>0){
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }
        result.setFlag(false);
        result.setMessage("fail");
        return result;
    }

    /**
     * 删除developer
     * @param did
     * @return result
     */
    @RequestMapping("/delete")
    public Result delete(@RequestParam("did") String did){
        Result result = new Result();
        int i = developerService.delete(did);
        if (i>0){
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }
        result.setFlag(false);
        result.setMessage("fail");
        return result;
    }
}
